/**
 * @author Carlos Ribeiro
 */
package br.jcrsistemas.factory;

import br.jcrsistemas.util.Configuracoes;
import java.sql.Connection;
import java.sql.SQLException;

import org.postgresql.ds.PGPoolingDataSource;

public class PostgresConnection {

    public static PostgresConnection myInstance;
    private static Connection conexao = null;
    PGPoolingDataSource source = new PGPoolingDataSource();

    private PostgresConnection() {
        source.setDataSourceName("PGSQLPOOL");
        source.setServerName(Configuracoes.getHost());
        source.setDatabaseName(Configuracoes.getDbName());
        source.setUser(Configuracoes.getUsuarioBanco());
        source.setPassword(Configuracoes.getSenhaBanco());
        source.setMaxConnections(Configuracoes.getMaxConnection());
    }

    public Connection abreConexao() throws SQLException {
        if (conexao == null) {
            conexao = source.getConnection();
        }
        System.out.println("Conexão aberta: Postgres");
        return conexao;
    }
    
    public void fechaConexao() throws SQLException {
        if(conexao != null ) {
            conexao.close();
        }
    }

    public static PostgresConnection getInstance() {
        if (myInstance == null) {
            myInstance = new PostgresConnection();
        }
        return myInstance;
    }
}
